<template>
  <el-dialog
    title="分配角色"
    :visible="isDialog"
    @close="hClose"
  >
    <!-- 复选框 -->
    <el-checkbox-group v-model="checkIds">
      <el-checkbox v-for="item in checkList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="hConfirm">确 定</el-button>
      <el-button @click="hClose">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getRoleList } from '@/api/role'
import { getDetails, updateRole } from '@/api/employee'
import { Message } from 'element-ui'
export default {
  props: {
    isDialog: {
      type: Boolean,
      required: true
    },
    characterId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      checkIds: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 确认
    async hConfirm() {
      await updateRole({ id: this.characterId, roleIds: this.checkIds })
      //   console.log(res)
      // 提示用户
      Message.success('修改员工角色成功')
      // 关闭弹窗
      this.hClose()
    },
    // 获取已启用角色列表
    async getRoleList() {
      const res = await getRoleList()
      //   console.log(res.data)
      this.checkList = res.data
    },
    // 获取员工详情
    async getRoleById() {
      const res = await getDetails(this.characterId)
      //   console.log(res)
      // 将获取的数据赋值给双向绑定的checkIds
      this.checkIds = res.data.roleIds
    },
    // 取消
    hClose() {
      this.$emit('update:isDialog', false)
    }
  }
}
</script>
